package com.iodemo.bio;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BioDemoApp {

    public static void main(String args[]) throws IOException {
        ServerSocket socketServer = new ServerSocket(9999);
        for(;;){
            System.out.println("等待连接 222222222");
            Socket socket = socketServer.accept();
            System.out.println("连接成功"+socket.getInetAddress().getHostAddress());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        handler(socket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    private static void handler(Socket socket) throws IOException {
        byte[] b = new byte[1024];
        InputStream in = socket.getInputStream();
        BufferedInputStream buff = new BufferedInputStream(in);
        int i = 0;
        while ((i = buff.read(b)) != -1){
            System.out.println(new String(b,0,i));
        }
        socket.getOutputStream().write("hello".getBytes());
        socket.getOutputStream().flush();
    }
}
